//
//  LOClassViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/19.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LOClassViewController.h"
#import "TypeDetailCell.h"
#import "LOGoodsViewController.h"

@interface LOClassViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) UIView *footerView;
@end

@implementation LOClassViewController
{
    UITableView *_tableView;
    UICollectionView *_collectionView;
    
    NSMutableArray *_leftNameArray;
    NSMutableArray *_leftIdArray;

    NSMutableArray *_sectionArr;
    NSMutableArray *_sectionNameArr;
    
    NSMutableArray *_detailArray;
    
    UITableViewCell *_cell;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"类目";
    _leftNameArray = [NSMutableArray array];
    _leftIdArray = [NSMutableArray array];
    _detailArray = [NSMutableArray array];
    _sectionArr = [NSMutableArray array];
    _sectionNameArr = [NSMutableArray array];
    
    [self downloadTopData];
    [self createFooterView];
    [self createTableView];
    [self createCollectionView];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)downloadTopData
{
    [self.view loadingOnAnyView];
    [THWManager GetHttp:API(LOGoodsClass) Parameters:nil Success:^(id responseObject) {
        [self.view removeAnyView];
        for (NSDictionary *dic in [Utils getData:responseObject]) {
            [_leftNameArray addObject:dic[@"catName"]];
            [_leftIdArray addObject:dic[@"catId"]];
            if ([dic[@"goodsCat2"] count] != 0) {
                [_sectionArr addObject:dic[@"goodsCat2"]];
            }
        }
        for (NSDictionary *dic in [_sectionArr objectAtIndex:0]) {
            [_sectionNameArr addObject:dic[@"catName"]];
            [_detailArray addObject:dic[@"goodsCat3"]];
        }
        [_tableView reloadData];
        [_collectionView reloadData];
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)downloadData:(NSString *)catId
{
    [_sectionNameArr removeAllObjects];
    [_detailArray removeAllObjects];
    [self.view loadingOnAnyView];
    [THWManager GetHttp:API(LOGoodsClassNext) Parameters:@{@"catId1":catId} Success:^(id responseObject) {
        [self.view removeAnyView];
        for (NSDictionary *dic in [Utils getData:responseObject]) {
            [_sectionNameArr addObject:dic[@"catName"]];
            [_detailArray addObject:dic[@"goodsCat3"]];
        }
        [_collectionView reloadData];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 100, HEIGHT-70) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView setTableFooterView:_footerView];
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _leftNameArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LOClassCell"];
    
    UILabel *label;
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LOClassCell"];
        label = Label(0, 15, 100, 20);
        label.textColor = [UIColor colorWithHex:0x333333];
        label.font = [UIFont systemFontOfSize:14.0f];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 555;
        cell.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
        [cell.contentView addSubview:label];
    }else{
        label = [cell.contentView viewWithTag:555];
    }
    label.text = _leftNameArray[indexPath.row];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    
    if (indexPath.row == 0) {
        _cell = cell;
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [self downloadData:String(_leftIdArray[indexPath.row])];
}

#pragma mark- collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(100, 70, WIDTH - 100, HEIGHT - 70) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"TypeDetailCell" bundle:nil] forCellWithReuseIdentifier:@"TypeDetailCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"HeaderReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self.view addSubview:_collectionView];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _sectionNameArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    UILabel *nameLabel = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        if (![reusableView viewWithTag:66]) {
            nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 50)];
            nameLabel.font = [UIFont systemFontOfSize:16];
            nameLabel.textColor = [UIColor colorWithHex:0x333333];
            [reusableView addSubview:nameLabel];
            nameLabel.tag = 66;
        }else{
            nameLabel = [reusableView viewWithTag:66];
        }
        
    }
    nameLabel.text = _sectionNameArr[indexPath.section];
    return reusableView;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={WIDTH,45};
    return size;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr = _detailArray[section];
    return arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TypeDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeDetailCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSArray *arr = _detailArray[indexPath.section];
    cell.label.text = [arr[indexPath.row] objectForKey:@"catName"];
    if (![TXUtilsString isBlankString:[arr[indexPath.row] objectForKey:@"img"]] ) {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.rmgs9.com/%@", [arr[indexPath.row] objectForKey:@"img"]]] placeholderImage:[UIImage imageNamed:@"orderOriginalImage"] options:SDWebImageRetryFailed];
    }else{
        [cell.imageView setImage:[UIImage imageNamed:@"orderOriginalImage"]];
    }
    return cell;
}

// 设置cell的水平间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

// 设置cell的竖直间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    float length = (WIDTH - 100) / 3;
    return CGSizeMake(length - 1, length - 1);
}

//定义每个UICollectionView 的 margin(边缘，范围)
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    // 上左下右的空隙
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = _detailArray[indexPath.section];
    LOGoodsViewController *goods = [[LOGoodsViewController alloc] init];
    goods.goodsId = [arr[indexPath.row] objectForKey:@"catId"];
    goods.goodsName = [arr[indexPath.row] objectForKey:@"catName"];
    [self.navigationController pushViewController:goods animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
